--
-- Author: ping.wang
-- Date: 2018-06-29 11:09:59
--
-- NewguildHomeLeftRender
--
local NewguildIconRender = import(".NewguildIconRender")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewGuildHomeLeftNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._guildName = self._ui.name -- 公会名字
    self._guildIcon = NewguildIconRender.create( self._ui.icon ) -- 公会头像

    local onlinePanel = self._ui.Panel_p
    display.uiAddClick( onlinePanel, function()
        local posTemp = onlinePanel:convertToWorldSpace(cc.p(0,0))
        local width, height = onlinePanel:getContentSize2()
        posTemp.x = posTemp.x + width
        executeFunc(self._onOperate, "showTips_onLine", posTemp )
    end )
    self._onlineMemberNum = onlinePanel:getChildByName( "num" ) -- 在线人数

    local lvPanel = self._ui.Panel_lv
    display.uiAddClick( lvPanel, function()
        local posTemp = lvPanel:convertToWorldSpace(cc.p(0,0))
        local width, height = lvPanel:getContentSize2()
        posTemp.x = posTemp.x + width
        executeFunc(self._onOperate, "showTips_Lv", posTemp )
    end )

    self._guildLv = lvPanel:getChildByName( "num" ) -- 等级
    local expBar = lvPanel:getChildByName( "guild_bar" ) -- 公会经验值
    expBar:setVisible( false )
    self._guildExp = cc.ProgressTimer:create( expBar )
    self._guildExp:setType( cc.PROGRESS_TIMER_TYPE_RADIAL )
    self._guildExp:setMidpoint(display.ANCHOR_POINTS[1])
    self._guildExp:setPosition( expBar:getPosition() )
    lvPanel:addChild( self._guildExp )


    local guildGoldPanel = self._ui.Panel_m
    display.uiAddClick( guildGoldPanel, function()
        local posTemp = guildGoldPanel:convertToWorldSpace(cc.p(0,0))
        local width, height = guildGoldPanel:getContentSize2()
        posTemp.x = posTemp.x + width
        executeFunc(self._onOperate, "showTips_donate", posTemp )
    end )

    self._guildCoinNum = guildGoldPanel:getChildByName( "num" ) -- 公会资金
    self._guildNotice = self._ui.Text -- 公告

    self._ui.Button_donate:addClickEventListener(function()
        executeFunc(self._onOperate, "openDonate")
    end)

    self._ui.Button_im:addClickEventListener(handler(self, self._onMsdkGroupBind))
    self:registerViewEvent({
        {name = ViewEvent.Msdk_GuildBindedChange, callback = handler(self, self.refreshMsdkGroupBinded)},
    })

    --跳转到公会排行界面的按钮
    self._ui.Button_rank:addClickEventListener(
        function()
            UIHandler:handleMessage({id = Constant.FUNC_TYPE_RANK,mode = "open", rankingId = Enums.GuildRankType.GuildLv})
        end
    )


    self:setContentSize(self._ui.Panel:getContentSize())
    self:refresh()
    self:refreshMsdkGroupBinded()
end

-- 刷新左侧基础信息
function ClassRef:refreshGuildInfo( myGuildInfo )
    self._guildName:setString( myGuildInfo:getGuildName() ) -- 公会名字
    self._guildIcon:setData( myGuildInfo:getGuildIcon() ) -- 公会头像

    local memberStr = table.concat( { myGuildInfo:getGuildMemberNum(), "/", myGuildInfo:getGuildMaxMemberNum() } )
    self._onlineMemberNum:setString( memberStr ) -- 公会人数/人数上限

    self._guildLv:setString( myGuildInfo:getGuildLv() ) -- 等级

    local lvUpNeedExp = tonumber( GD:queryGuildLvById( myGuildInfo:getGuildLv() ).exp or 0 ) -- 升级所需经验值
    if lvUpNeedExp > 0 then -- 存在下一级？
        self._guildExp:setPercentage( math.ceil( myGuildInfo:getGuildExp() / lvUpNeedExp * 100 ) )
    else
        self._guildExp:setPercentage( 100 ) -- 经验值进度条
    end

    self._guildCoinNum:setString( myGuildInfo:getGuildFund() ) -- 公会资金

    local guildNotice = myGuildInfo:getGuildNoticeContent()
    if "" ~= guildNotice then
        self._guildNotice:setString( guildNotice ) -- 公告
    end
end

function ClassRef:refresh()
    
end

function ClassRef:onOperate( cb )
    self._onOperate = cb
end


function ClassRef:_onMsdkGroupBind()
    if not app:isTencent() then
        return
    end

    local nowTime = app.session:getServerTime()
	if self._clickGroupTime then
		if nowTime - self._clickGroupTime <= 2 then
			return
		end
	end
	self._clickGroupTime = nowTime

    local joinState = UD.msdkData:getGroupJoinedState()
    if joinState == Enums.MsdkGroupJoinState.Joined then
        display.pushToast(L("AlreadyJoinGroupTip"))
    elseif joinState == Enums.MsdkGroupJoinState.NotBinded then
        display.pushToast(L("NotBindGroupTip"))
    elseif joinState == Enums.MsdkGroupJoinState.Unjoin then
        UD.msdkData:joinGroup()
    end
end

function ClassRef:refreshMsdkGroupBinded()
    if not app:isTencent() then
        return
    end
    local joinState = UD.msdkData:getGroupJoinedState()
    local wgPlatform = app.channelAPI:getWGPlatform()
    local width = self._ui.Panel:getContentSize2()
    if Enums.MSDK_PLATFORM.QQ == wgPlatform then
        local isGroupBtnShow = joinState == Enums.MsdkGroupJoinState.Unjoin or joinState == Enums.MsdkGroupJoinState.Joined or joinState == Enums.MsdkGroupJoinState.NotBinded
        self._ui.Button_im:setVisible(isGroupBtnShow)
        if isGroupBtnShow then
          
            self._ui.Button_im:setPositionX(0.7 * width)
            self._ui.Button_donate:setPositionX(0.3 * width)
            if  joinState == Enums.MsdkGroupJoinState.Unjoin then
                self._ui["Button_im/name"]:setString(L("MSDK_JOIN_GROUP"))
            elseif joinState == Enums.MsdkGroupJoinState.Joined then
                self._ui["Button_im/name"]:setString(L("MSDK_JOINED_GROUP"))
            elseif joinState == Enums.MsdkGroupJoinState.NotBinded then
                self._ui["Button_im/name"]:setString(L("MSDK_NOTBIND_GROUP"))
            end
        else
            self._ui.Button_donate:setPositionX(0.5 * width)
        end
    elseif Enums.MSDK_PLATFORM.WX == wgPlatform then
        local isGroupBtnShow = joinState == Enums.MsdkGroupJoinState.Unjoin or joinState == Enums.MsdkGroupJoinState.Joined or joinState == Enums.MsdkGroupJoinState.NotBinded
        self._ui.Button_im:setVisible(isGroupBtnShow)
        if isGroupBtnShow then
            self._ui.Button_im:setPositionX(0.7 * width)
            self._ui.Button_donate:setPositionX(0.3 * width)

            if  joinState == Enums.MsdkGroupJoinState.Unjoin then
                self._ui["Button_im/name"]:setString(L("MSDK_JOIN_GROUP"))
            elseif joinState == Enums.MsdkGroupJoinState.Joined then
                self._ui["Button_im/name"]:setString(L("MSDK_JOINED_GROUP"))
            elseif joinState == Enums.MsdkGroupJoinState.NotBinded then
                self._ui["Button_im/name"]:setString(L("MSDK_NOTBIND_GROUP"))
            end
        else
            self._ui.Button_donate:setPositionX(0.5 * width)
        end
    end
end

return ClassRef
